package com.xiaoyezi.demo2.Controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xiaoyezi.demo2.DataSource.Shop;
import com.xiaoyezi.demo2.DataSource.User;
import com.xiaoyezi.demo2.base.Status;
import com.xiaoyezi.demo2.service.impl.ShopService;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin(value = "http://localhost:1024",allowCredentials = "true")
public class shopController {
    // 注入ShopService实现类
    @Autowired
    ShopService shopService;
    // 返回状态
    Status s = new Status();
    
    // 根据ID查询数据
    @PostMapping("getIdShop")
    public Status getById(@RequestBody Shop shop){
        Shop aa = shopService.getById(shop);
        System.out.println(shop);
        return s.success(aa);
    }
    // 插入商品数据
    @PostMapping("insertShop")
    public Status insertShop(@RequestBody Shop shop){
        shopService.insertShop(shop);
        System.out.println(shop);
        return s.success();
    }
    // 查询随机商品数据
    @PostMapping("getItemsData")
    public Status getItemsData(@RequestBody Shop shop){
        List<Shop> q = shopService.shopRandom(shop);
        System.out.println(q);
        return s.success(q);
    }
    // 销量最高数据
    @GetMapping("getMaxSales")
    public Status getBySales(){
        List<Shop>  sales = shopService.getBySales();
        return s.success(sales);
    }
    // 更新商品数据
    @PostMapping("updateShop")
    public Status updateShop(@RequestBody Shop shop){
        if (shop == null){
            return s.error("请输入数据！");
        }else {
            shopService.updateShop(shop);
        return s.success();
        }
        
    }
    // 获取用户的所有商品
    @PostMapping("getUserAllShop")
    public Status getUserAllShop(@RequestBody Shop shop){
        List<Shop> getShops = shopService.getUserAllShop(shop);
        System.out.println(shop);
        return s.success(getShops);
    }
    // 商品图片
    @PostMapping("updateImage")
    public Status updateImage(@RequestParam("file") MultipartFile file,Shop shop,HttpServletRequest request) {
        System.out.println("传输文件开始");
        // 判断接收的文件是否存在
        if (file.isEmpty()) {
            return s.error("图片上传失败1");
        }

        // 文件校验重命名（a:1.png  b:1.png）
        String originalFilename = file.getOriginalFilename(); // 原来的图片名
        String ext = "." + originalFilename.split("\\.")[1]; // 提取图片后缀并添加.(1.png)
        String uuid = UUID.randomUUID().toString().replace("-", "");
        // 新的图片名(bb06c72eb3dc4262935375c0f2678d62.jpg)
        String fileName = uuid + ext;

        // 文件的保存路径
        String uploadDir = "C:/Users/xyz34/Documents/java/java-springboot-project/demo2/src/main/resources/static/images";
        // 将文件的保存路径和新文件名组合起来，形成完整的文件路径
        String path = uploadDir + File.separator + fileName;
        // 在控制台打印出完整的文件路径，用于调试和确认文件将被保存的确切位置
        System.out.println(path);

        try {
            file.transferTo(new File(path));
            System.out.println("传输文件结束");
            // 返回HTTP URL
            String imageUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/images/" + fileName;
            // 将imageUrl存储到数据库中
            shop.setImage(imageUrl);
            shopService.updateShop(shop);
            // 查询数据并返回
            Shop getByIdImage = shopService.getById(shop);
            return s.success(getByIdImage);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s.error("图片上传失败2");
    }
}



